package com.liusany.myshop.web.admin;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.liusany.modules.orm.Page;
import com.liusany.modules.orm.PropertyFilter;
import com.liusany.myshop.bean.propertyeditor.RoleListEditor;
import com.liusany.myshop.entity.Admin;
import com.liusany.myshop.service.AccountService;

/**
 * Urls: List page : GET /account/user/ Create page : GET /account/user/create
 * Create action : POST /account/user/save Update page : GET
 * /account/user/update/{id} Update action : POST /account/user/save/{id} Delete
 * action : POST /account/user/delete/{id} CheckLoginName ajax: GET
 * /account/user/checkLoginName?oldLoginName=a&loginName=b
 * 
 * @author calvin
 * 
 */
@Controller
@RequestMapping(value = "/admin/user")
public class UserController {
	
	private static Logger logger = Logger.getLogger(UserController.class);
	
	@Resource
	private AccountService accountService;
	@Resource
	private RoleListEditor roleListEditor;

	
	@InitBinder
	public void initBinder(WebDataBinder b) {
		b.registerCustomEditor(List.class, "groupList", roleListEditor);
	}

	/**
	 * 用户列表
	 * @param model
	 * @param pager
	 * @return
	 */
	@RequestMapping(value = { "list", "" })
	public String list(Model model,Page pager,HttpServletRequest request) {
		try {
			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
			//设置默认排序方式
			if (!pager.isOrderBySetted()) {
				pager.setOrderBy("id");
			}
			pager = accountService.findByPage(pager, filters);
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("pager", pager);
		return "admin/admin_list";
	}

	/**
	 * 新建用户from表单
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("admin", new Admin());
		model.addAttribute("allRoles", accountService.getAllRole());
		return "admin/adminForm";
	}
	

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Admin admin, RedirectAttributes redirectAttributes) {
		accountService.saveAdmin(admin);
		redirectAttributes.addFlashAttribute("message",
				"创建用户" + admin.getUsername() + "成功");
		return "redirect:/admin/user/";
	}

	@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
	public String delete(@PathVariable("id") Long id,
			RedirectAttributes redirectAttributes) {
		accountService.deleteUser(id);
		redirectAttributes.addFlashAttribute("message", "删除用户成功");
		return "redirect:/admin/user/";
	}

	@RequestMapping(value = "checkLoginName", method = RequestMethod.GET)
	@ResponseBody
	public String checkLoginName(
			@RequestParam("oldLoginName") String oldLoginName,
			@RequestParam("loginName") String loginName) {
		if (loginName.equals(oldLoginName)) {
			return "true";
		} else if (accountService.findUserByLoginName(loginName) == null) {
			return "true";
		}

		return "false";
	}

}
